ifeq ($(APP_PARAM), )
    APP_PARAM:=../Makefile.param
    include $(APP_PARAM)
endif

export LC_ALL=C
SHELL:=/bin/bash

CURRENT_DIR := $(shell pwd)

PKG_NAME_APP := rk_chatgpt
PKG_NAME_CHATGPT := rk_chatgpt
PKG_CONF := conf
PKG_BIN := out
PKG_BUILD := build

RK_APP_OPTS = -Wl,-rpath-link,$(RK_APP_MEDIA_LIBS_PATH):${RK_APP_OUTPUT}/lib:$(RK_APP_OUTPUT)/root/usr/lib $(RK_APP_CROSS_CFLAGS)

RK_APP_OPTS += -I $(RK_APP_MEDIA_INCLUDE_PATH) \
				-I $(RK_APP_MEDIA_INCLUDE_PATH)/libdrm \
				-I $(RK_APP_MEDIA_INCLUDE_PATH)/rkaiq \
				-I $(RK_APP_MEDIA_INCLUDE_PATH)/rkaiq/uAPI \
				-I $(RK_APP_MEDIA_INCLUDE_PATH)/rkaiq/uAPI2 \
				-I $(RK_APP_MEDIA_INCLUDE_PATH)/rkaiq/algos \
				-I $(RK_APP_MEDIA_INCLUDE_PATH)/rkaiq/common \
				-I $(RK_APP_MEDIA_INCLUDE_PATH)/rkaiq/xcore \
				-I $(RK_APP_MEDIA_INCLUDE_PATH)/rkaiq/iq_parser \
				-I $(RK_APP_MEDIA_INCLUDE_PATH)/rkaiq/iq_parser_v2 \
				-I $(RK_APP_OUTPUT)/include \
				-I $(RK_APP_OUTPUT)/include/freetype2 \
				-I $(RK_APP_MEDIA_INCLUDE_PATH)/easymedia

PKG_CONF_OPTS += $(RK_APP_OPTS)
PKG_CONF_OPTS += -DRKPLATFORM=ON

ifeq ($(RK_APP_CHIP), rv1126)
PKG_CONF_OPTS += -DCMAKE_SYSTEM_PROCESSOR=armv7l
PKG_CONF_OPTS += -DARCH64=OFF
endif

ifeq ($(RK_APP_CHIP), rv1106)
PKG_CONF_OPTS += -DCMAKE_SYSTEM_PROCESSOR=armv7l
PKG_CONF_OPTS += -DARCH64=OFF
endif

ifeq ($(RK_APP_CHIP), rk3588)
PKG_CONF_OPTS += -DARCH64=ON
endif

#ifeq ($(RK_ENABLE_CHATGPT),y)
#PKG_TARGET := nlssdk-build chatgpt-build
PKG_TARGET := chatgpt-build
#else
#PKG_TARGET :=
#$(warning Not config source chatgpt, Skip...)
#endif

ifeq ($(PKG_BIN),)
$(error ### $(CURRENT_DIR): PKG_BIN is NULL, Please Check !!!)
endif


PKG_CONF_OPTS +=  -I $(CURRENT_DIR)/include -I $(CURRENT_DIR)/../wifi_app/wifi/include -I $(CURRENT_DIR)/../facial_gate/facial_gate/libs/libcurl/include -I $(RK_APP_OUTPUT)/include 
PKG_CONF_OPTS +=  -I $(RK_TOOLS_OUTPUT)/zlib/out/include -I $(RK_TOOLS_OUTPUT)/openssl/out/include
PKG_CONF_OPTS +=  -fPIC -fdata-sections -ffunction-sections -Wno-unused-function -fpermissive -Wno-narrowing -DARCH="$(RK_APP_ARCH_TYPE)"
PKG_CONF_OPTS +=  -O0 -fPIC -g -ggdb 
#PKG_CONF_OPTS +=  -O3 -fPIC 
PKG_CONF_OPTS += -D_GLIBCXX_USE_CXX11_ABI=1 -std=c++11 
RK_APP_LDFLAGS = -L $(RK_APP_MEDIA_LIBS_PATH) \
                 -L $(RK_APP_OUTPUT)/lib \
				 -L $(CURRENT_DIR)/lib \
				 -L $(CURRENT_DIR)/../facial_gate/facial_gate/libs/libcurl/lib \
				 -L $(CURRENT_DIR)/../component/out/lib \
				 -L $(CURRENT_DIR)/../component/third_libs/lib/arm-rockchip830-linux-uclibcgnueabihf \
				 -L $(RK_TOOLS_OUTPUT)/openssl/out/lib \
				 -L $(RK_TOOLS_OUTPUT)/zlib/out/lib \
				 -lm -lpthread -liconv -lssl -lcrypto -lz -lstdc++ -lrksysutils -lcurl \
				 -lrockchip_mpp -lrkaiq -lrtsp -lrkaudio_detect -lrkaudio -lrkdemuxer -laec_bf_process -lrkmuxer -lrockiva -lrknnmrt -lrga \
				 -lalibabacloud-idst-speech -levent_core -levent_extra -levent_pthreads -llog4cpp -logg -lopus -ljsoncpp -luuid -Wl,-Map=rk_chatgpt.map

ifeq ($(RK_APP_CHIP), rv1106)
RK_APP_LDFLAGS += -lrockit_full -ldrm 
else
RK_APP_LDFLAGS += -lrockit 
endif


all: $(PKG_TARGET)
	@echo "build $(PKG_TARGET) app done"

chatgpt-build: 
	@test -f $(CURRENT_DIR)/$(PKG_BIN)/$(PKG_NAME_APP) || (\
	echo "build chatgpt app start"; \
	mkdir -p $(CURRENT_DIR)/$(PKG_BIN)/bin; \
	mkdir -p $(CURRENT_DIR)/$(PKG_BUILD); \
	pushd $(CURRENT_DIR)/$(PKG_NAME_CHATGPT)/; \
		CC="$(RK_APP_CROSS)-gcc" GCC="$(RK_APP_CROSS)-gcc" CXX="$(RK_APP_CROSS)-g++" CPP="$(RK_APP_CROSS)-cpp" \
		CFLAGS="$(PKG_CONF_OPTS)" CPPFLAGS="$(PKG_CONF_OPTS)" \
		CXXFLAGS="$(PKG_CONF_OPTS)" LDFLAGS="$(RK_APP_LDFLAGS)" \
		make  -j$(RK_APP_JOBS) || exit -1; \
		cp -f $(CURRENT_DIR)/$(PKG_NAME_CHATGPT)/$(PKG_NAME_APP) $(CURRENT_DIR)/$(PKG_BIN)/bin/$(PKG_NAME_APP) ; \
		cp -rf $(CURRENT_DIR)/$(PKG_CONF) $(CURRENT_DIR)/$(PKG_BIN)/bin/$(PKG_CONF); \
	popd; )
	$(call MAROC_COPY_PKG_TO_APP_OUTPUT, $(RK_APP_OUTPUT), $(PKG_BIN))

nlssdk-build:
	@test ! -d $(RK_APP_COMPONENT)/nlssdk || make -C $(RK_APP_COMPONENT)/nlssdk

minimp3-build:
	@test ! -d $(RK_APP_COMPONENT)/minimp3 || make -C $(RK_APP_COMPONENT)/minimp3

clean: distclean

distclean:
	@make -C $(CURRENT_DIR)/$(PKG_NAME_CHATGPT)/ clean;
	@rm -rf $(PKG_BIN);
